// pages/test/test.js
import instance from '../../utils/http'
import {setStorage} from '../../utils/storage'
import {reLogin, reqUserInfo } from '../../api/user'
// 导入 ComponentWithStore 方法
import { ComponentWithStore} from'mobx-miniprogram-bindings'
//导入store对象
import { userStore} from '../../stores/userstore'

ComponentWithStore({

  storeBindings:{
    //让页面和store对象建立联系
    store:userStore,
    fields:['token','userInfo'],
    actions:['setToken','setUserInfo']
  },
// 授权登录
// 账号密码
data:{
  loginData: {
    username: 'Jocelyn',
    password: '1234567890'
  },
  articles:[
    {
      id: 1,
      title:'你好',
      coverImg:'https://webzt.oss-cn-beijing.aliyuncs.com/5d1a9b61-179c-418f-b33e-eed4b7afb261.jpeg',
      content:'<p>天安门,颐和园,鸟巢,长城...爱去哪去哪...</p>',
      state:'已发布',
      categoryId:'2',
      createtime:'33',
      updateTime:'333'
    }
  ]
},

methods:{
  // 登录请求，将token存到本地
async login () {
  const loginData = this.data.loginData;
  // const formData = Object.keys(loginData)
  // .map(key => encodeURIComponent(key) + '=' + encodeURIComponent(loginData[key]))
  // .join('&');
const res = await reLogin(loginData)

console.log(res);
setStorage('token',res.data)
//将token传入到Store
this.setToken(res.data)
},
//获取文章数据
async getArticle(){
  const res = await instance.get('/article/mlist')
  console.log(res)
  this.setData({
    articles:res.data
  })
  console.log(this.data.articles);
},

//获取体重信息
async getWeightInfo(){

  //调用接口获取信息
const {data} = await reqUserInfo()
console.log(data)
//将信息存入本地
setStorage('userInfo',data)
//将信息存储到Store对象
this.setUserInfo(data)
},
},

})